from __future__ import division
import time
import Adafruit_PCA9685
import gui

pwm = Adafruit_PCA9685.PCA9685()
# pwm2 = Adafruit_PCA9685.PCA9685(address=0x40)

# Configure min and max servo pulse lengths
servo_min = 150  # Min pulse length out of 4096
servo_max = 600  # Max pulse length out of 4096

# Set frequency to 60hz, good for servos.
pwm.set_pwm_freq(60)
# pwm2.set_pwm_freq(60)


def init():
    pwm.set_pwm(1,0,220)

    pwm.set_pwm(2,0,240)
    pwm.set_pwm(3,0,120)
    pwm.set_pwm(4,0,220)
    pwm.set_pwm(5,0,150)
    
def gar_pull(label):
    time.sleep(1)
    pwm.set_pwm(1,0,220+label*130)
    time.sleep(2)
    pwm.set_pwm(4,0,410)
    time.sleep(1)
    pwm.set_pwm(4,0,220)

def pull_gar(labels,label_id,prob):
    thr_value=0.92
    try:
        if label_id==0 and prob >thr_value:
            gui.window['gar1'].update(labels[label_id])
            pwm.set_pwm(1,0,220)
            pwm.set_pwm(4,0,410)
            time.sleep(1)
            pwm.set_pwm(4,0,220)
        if label_id==1 and prob >thr_value:
            gui.window['gar2'].update(labels[label_id])
            gar_pull(1)
            time.sleep(1)
            init()
        if label_id==2 and prob>thr_value:
            gui.window['gar3'].update(labels[label_id])
            gar_pull(2)
            time.sleep(1)
            init()
        if label_id==3 and prob>thr_value:
            gui.window['gar4'].update(labels[label_id])
            gar_pull(3)
            time.sleep(1)
            init()
    except Exception as e:
        print("erro")